package net.loonggg.bar;

import io.netty.handler.codec.spdy.SpdySessionHandler;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.op.teachingliteapp.R;

// TODO Auto-generated method stub
public class ListViewAdapter extends BaseAdapter {
	private Context context;
	private List<Course> list;
	private ViewHolder viewHolder;
	public static StringBuffer checkCourseName = new StringBuffer();
	public static List<String> courseList = new ArrayList<String>();
	public static List<String> getCourseList() {
		return courseList;
	}

	public LinearLayout itemLayout;
	private int size;

	public ListViewAdapter(Context context, List<Course> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		if (list.get(position).getName().length() == 1)
			return false;
		return super.isEnabled(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		String item = list.get(position).getName();
		viewHolder = new ViewHolder();
		if (item.length() == 1) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_course_add_index, null);
			viewHolder.indexTv = (TextView) convertView
					.findViewById(R.id.indexTv);
		} else {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_course_add, null);
			viewHolder.itemTv = (TextView) convertView
					.findViewById(R.id.itemTv);
			viewHolder.item_cb = (CheckBox) convertView
					.findViewById(R.id.item_cb);
			// 添加课程添加的勾选按钮响应
			viewHolder.item_cb
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (isChecked) {
								// 选中之后将选中的科目名添加到缓冲字符串里
								checkCourseName.append(list.get(position)
										.getName());

								courseList.add(list.get(position).getName());

							} else {
								size = checkCourseName.indexOf(list.get(
										position).getName());
								checkCourseName.delete(size,
										size
												+ list.get(position).getName()
														.length());
								for (int i = 0; i < courseList.size(); i++) {

									if (courseList.get(i) == list.get(position)
											.getName()) {

										courseList.remove(i);

										i--;

									}

								}
							}
						}
					});
		}
		if (item.length() == 1) {
			viewHolder.indexTv.setText(list.get(position).getName());
		} else {
			viewHolder.itemTv.setText(list.get(position).getName());
		}

		return convertView;
	}

	private class ViewHolder {
		private CheckBox item_cb;
		private TextView indexTv;
		private TextView itemTv;
	}

}
